<?php


class Poster
{
    public function addPic($path_base, $path_logo, $imgWidth, $imgHeight, $dst_x, $dst_y, $new)
    {
        $image_base = self::ImgInfo($path_base);
        $image_logo = self::ImgInfo($path_logo);
//        print_r($image_logo);die;
//        print_r($image_logo);die;
        imagecopyresampled($image_base, $image_logo, $dst_x, $dst_y, 0, 0, $imgWidth, $imgHeight, imagesx($image_logo), imagesy($image_logo));
        // 生成一个合并后的新图
        imagejpeg($image_base, $new);
        // 载入新图像资源
        $new_pic = imagecreatefromjpeg($new);
        // 生成写入文字的的新图
        imagejpeg($new_pic, $new);
    }

    public function addWord($str, $posX, $poxY, $font, $color, $new)
    {
        $ori_img = $new;    //原图
        $new_img = $new;    //生成水印后的图片

        $s_original = self::ImgInfo($ori_img);
        $tilt = 0;    //文字的倾斜度
        $ImgColor = [                    //为一幅图像分配颜色
            'black' => imagecolorallocate($s_original, 0, 0, 0),
            'red' => imagecolorallocate($s_original, 255, 0, 0),
        ];

        imagettftext($s_original, $font, $tilt, $posX, $poxY, $ImgColor[$color], __DIR__ . '/pingfang.ttf', $str);

        $loop = imagejpeg($s_original, $new_img);    //生成新的图片(jpg格式)

    }

    public function ImgInfo($img)
    {
        /*判断是否是http  https 图片链接*/
        if (preg_match('/http(s)?:\/\//', $img)) {
            $fileSuffix = self::getNetworkImgType($img);
        } else {
            /*获取图片后缀*/
            $ename = getimagesize($img);
            $ename = explode('/', $ename['mime']);
            $fileSuffix = $ename[1];
        }
        if (!$fileSuffix) return false;
        switch ($fileSuffix) {
            case 'jpeg':
                $theImage = @imagecreatefromjpeg($img);
                break;
            case 'jpg':
                $theImage = @imagecreatefromjpeg($img);
                break;
            case 'png':
                try {
                    $theImage = imagecreatefrompng($img);
                } catch (\Exception $e) {
                    $theImage = imagecreatefromstring(file_get_contents($img));
                }
                break;
            case 'gif':
                $theImage = @imagecreatefromgif($img);
                break;
            default:
                $theImage = @imagecreatefromstring(file_get_contents($img));
                break;
        }
        return $theImage;
    }

    public function getNetworkImgType($url)
    {
        $http_code = $this->curlHeader($url);
        if ($http_code['http_code'] == 200) {
            $theImgType = explode('/', $http_code['content_type']);
            if ($theImgType[0] == 'image') {
                return $theImgType[1];
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function cn_row_substr($str, $row = 1, $number = 10, $suffix = true)
    {
        $result = array();
        for ($r = 1; $r <= $row; $r++) {
            $result[$r] = '';
        }

        $str = trim($str);
        if (!$str) return $result;

        $theStrlen = strlen($str);

        //每行实际字节长度
        $oneRowNum = $number * 3;
        for ($r = 1; $r <= $row; $r++) {
            if ($r == $row and $theStrlen > $r * $oneRowNum and $suffix) {
                $result[$r] = self::mg_cn_substr($str, $oneRowNum - 6, ($r - 1) * $oneRowNum) . '...';
            } else {
                $result[$r] = self::mg_cn_substr($str, $oneRowNum, ($r - 1) * $oneRowNum);
            }
            if ($theStrlen < $r * $oneRowNum) break;
        }

        return $result;
    }

    public function mg_cn_substr($str, $len, $start = 0)
    {
        $q_str = '';
        $q_strlen = ($start + $len) > strlen($str) ? strlen($str) : ($start + $len);

        //如果start不为起始位置，若起始位置为乱码就按照UTF-8编码获取新start
        if ($start and json_encode(substr($str, $start, 1)) === false) {
            for ($a = 0; $a < 3; $a++) {
                $new_start = $start + $a;
                $m_str = substr($str, $new_start, 3);
                if (json_encode($m_str) !== false) {
                    $start = $new_start;
                    break;
                }
            }
        }

        //切取内容
        for ($i = $start; $i < $q_strlen; $i++) {
            //ord()函数取得substr()的第一个字符的ASCII码，如果大于0xa0的话则是中文字符
            if (ord(substr($str, $i, 1)) > 0xa0) {
                $q_str .= substr($str, $i, 3);
                $i += 2;
            } else {
                $q_str .= substr($str, $i, 1);
            }
        }
        return $q_str;
    }

    public function curlHeader($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
        curl_exec($ch);//执行curl会话
        $return = curl_getinfo($ch);//获取curl连接资源句柄信息
        curl_close($ch);
        return $return;
    }
}